package com.cimr.comm.exception;

import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;
import com.cimr.comm.dto.HttpResult;
import com.cimr.comm.util.AjaxUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.UnauthenticatedException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.SimpleMappingExceptionResolver;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 全局异常类
 * Created by liqi on 2017/8/4.
 */
public class GlobalExceptionResolver extends SimpleMappingExceptionResolver {
    private static final Log log = LogFactory.get();

    @Override
    protected ModelAndView doResolveException(HttpServletRequest request,
                                              HttpServletResponse response, Object handler, Exception ex){

        if (!SecurityUtils.getSubject().isAuthenticated()) {
            if (AjaxUtil.isAjax(request)) {
                HttpResult result = new HttpResult(false, ex.getMessage());
                if (ex instanceof UnauthenticatedException) {
                    result.setError("UnauthenticatedException");
                }
                AjaxUtil.out(response, result);
                return null;
            } else if (ex instanceof SsoException) {
                try {
                    throw ex;
                } catch (Exception e) {
                    e.printStackTrace();
                }
            } else {
                try {
                    throw new NotLoginException("请重新登录。");
                } catch (NotLoginException e) {
                    e.printStackTrace();
                }
                return getModelAndView("comm/exception/loginException", ex, request);
            }
        }

        String viewName = determineViewName(ex,request);
        response.setCharacterEncoding("UTF-8");
        if (viewName != null) {// JSP格式返回
            if (!AjaxUtil.isAjax(request)) {
                // 如果不是异步请求
                // Apply HTTP status code for error views, if specified.
                // Only apply it if we're processing a top-level request.
                Integer statusCode = determineStatusCode(request, viewName);
                if (statusCode != null) {
                    applyStatusCodeIfPossible(request, response, statusCode);
                }
                System.out.println("JSP格式返回" + viewName);
                return getModelAndView(viewName, ex, request);
            } else {// JSON格式返回
//                try {
                    HttpResult result = new HttpResult(false, ex.getMessage());
                    AjaxUtil.out(response, result);
//                    if (ex instanceof NotLoginException) {
//                    } else {
//                        AjaxUtil.out(response, result);
//
//                        PrintWriter writer = response.getWriter();
//                        writer.write(ex.getMessage());
//                        writer.flush();
//                    }
//                } catch (IOException e) {
//                    e.printStackTrace();
//                }

                log.info(ex);
                System.out.println("JSON格式返回" + viewName);
                return null;
            }
        } else {
            return null;
        }
    }
}
